﻿using System;
using System.Collections.Generic;
using System.Linq;
using Citi.EmailSender.Domain.Utils;

namespace Citi.EmailSender.Domain
{
    public partial class Email
    {
        public List<Email> GetPendingEmails()
        {
            Context.SetOverwriteChanges(Context.Instance.Emails);
            return SelectAll<Email>()
                    .Where(x => x.SentOn == null && x.Tentatives < 3 && x.Approved.HasValue && x.Approved.Value
                        && (x.ScheduleDate == null || x.ScheduleDate <= DateTime.Now)).ToList();
        }

        public List<Email> GetEmailByUserApproval(string soeid, bool? approved)
        {
            return SelectAll<Email>()
                    .Where(x => x.Approved == approved
                        && x.EmailReceivers.Any(y => y.EmailAddress.Address.ToUpper() == soeid.ToUpper() && y.Type == (int)Enums.EmailReceiverType.Approver)).ToList();
        }
    }
}
